<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>定时系统 - Quartz Scheduler System</title>
    <link rel="stylesheet" href="${ctxPath}/css/common.css"/>
    <script src="${ctxPath}/js/common.js"></script>
    <script src="${ctxPath}/js/jquery-1.11.3.min.js"></script>
</head>
<script>
/**
 * 新增定时任务时的表单校验
 */
function formValidate(){
    if(isEmpty($("#dynamicPassword").val())){
        alert("动态密码不能为空");
        return false;
    }
    if($("#dynamicPassword").val() == "请输入动态密码（动态密码请联系玄玉）"){
        alert("请输入动态密码");
        return false;
    }
    if(isEmpty($("#name").val())){
        alert("任务名称不能为空");
        return false;
    }
    if(isEmpty($("#url").val())){
        alert("任务URL不能为空");
        return false;
    }
    if(isEmpty($("#cron").val())){
        alert("任务Cron表达式不能为空");
        return false;
    }
    return true;
}

/**
 * 新增定时任务
 */
function addTask(){
    if(formValidate()){
        $.post("${ctxPath}/qss/add",
                {status:${@com.jadyer.seed.comm.constant.SeedConstants.QSS_STATUS_STOP}, name:$("#name").val(), url:$("#url").val(), cron:$("#cron").val(), dynamicPassword:$("#dynamicPassword").val()},
                function(jsonData){
                    if(0 == jsonData.code){
                        location.reload();
                    }else{
                        alert(jsonData.msg);
                    }
                }
        );
    }
}

/**
 * 删除定时任务
 */
function deleteTask(id){
    if(inputDynamicPassword()){
        if(confirm("确定删除此任务么？")){
            $.get("${ctxPath}/qss/delete/"+id+"/"+dynamicPassword,function(data){
                if(0 == data.code){
                    location.reload();
                }else{
                    alert(data.msg);
                }
            });
        }
    }
}

/**
 * 立即执行定时任务
 */
function triggerJob(id){
    if(inputDynamicPassword()){
        if(confirm("确定立即执行此任务么？")){
            $.get("${ctxPath}/qss/triggerJob/"+id+"/"+dynamicPassword,function(data){
                if(0 == data.code){
                    location.reload();
                }else{
                    alert(data.msg);
                }
            });
        }
    }
}

/**
 * 停止0/启动1/挂起2/恢复3定时任务
 */
function updateStatus(id, status){
    if(inputDynamicPassword()){
        $.get("${ctxPath}/qss/updateStatus?id="+id+"&status="+status+"&dynamicPassword="+dynamicPassword,function(data){
            if(0 == data.code){
                location.reload();
            }else{
                alert(data.msg);
            }
        });
    }
}

/**
 * 更新定时任务Cron表达式
 */
function updateCron(id, cron){
    if(inputDynamicPassword()){
        var cron = prompt("请输入CronExpression", cron);
        if(cron){
            $.get("${ctxPath}/qss/updateCron?id="+id+"&cron="+cron+"&dynamicPassword="+dynamicPassword,function(data){
                if(0 == data.code){
                    location.reload();
                }else{
                    alert(data.msg);
                }
            });
        }
    }
}

/**
 * 提示用户输入动态密码
 * @see http://tool.oschina.net/encrypt?type=2
 */
var dynamicPassword = null;
function inputDynamicPassword(){
    var _dynamicPassword = prompt("请输入动态密码", "动态密码请联系玄玉");
    if(!isEmpty(_dynamicPassword) && _dynamicPassword!="动态密码请联系玄玉"){
        dynamicPassword = _dynamicPassword;
        return true;
    }else{
        return false;
    }
}
</script>
<body>
    <div align="center">
        <h1><span style="color:red;">定时系统（QSS--Quartz Scheduler System）</span></h1>
        <br>
        <table border="9">
            <tr>
                <th style="width:10%;">应用</th>
                <th style="width:10%;">任务</th>
                <th style="width:3%;">状态</th>
                <th style="width:25%;">URL</th>
                <th style="width:10%;">Cron</th>
                <th style="width:11%;">下次触发时间</th>
                <th style="width:11%;">上次触发时间</th>
                <th style="width:18%;">操作</th>
            </tr>
            <%for(task in taskList){%>
                <tr>
                    <td>${task.appname}</td>
                    <td>${task.name}</td>
                    <td>${task.status==@com.jadyer.seed.comm.constant.SeedConstants.QSS_STATUS_RUNNING ? '启动' : '停止'}</td>
                    <td style="word-break:break-all;">${task.url}</td>
                    <td>${task.cron}</td>
                    <td>${task.nextFireTime, dateFormat="yyyy-MM-dd HH:mm:ss"}</td>
                    <td>${task.previousFireTime, dateFormat="yyyy-MM-dd HH:mm:ss"}</td>
                    <td>
                        <%if(task.status==0){%>
                            <a href="javascript:updateStatus(${task.id}, ${@com.jadyer.seed.comm.constant.SeedConstants.QSS_STATUS_RUNNING});">启动</a>
                            立即执行
                        <%}%>
                        <%if(task.status==1){%>
                            <a href="javascript:updateStatus(${task.id}, ${@com.jadyer.seed.comm.constant.SeedConstants.QSS_STATUS_STOP});">停止</a>
                            <a href="javascript:triggerJob(${task.id});">立即执行</a>
                        <%}%>
                        <a href="javascript:updateCron(${task.id}, '${task.cron}');">更新Cron</a>
                        <a href="javascript:deleteTask(${task.id});">删除</a>
                    </td>
                </tr>
            <%}%>
            <tr>
                <td><input name="name" id="name" size="16"/></td>
                <td>停止</td>
                <td><input name="url" id="url" size="32"/></td>
                <td><input name="cron" id="cron" value="50 * * * * ?" size="16"/></td>
                <td colspan="2"><input name="dynamicPassword" id="dynamicPassword" value="请输入动态密码（动态密码请联系玄玉）" size="34"/></td>
                <td><input type="button" onclick="addTask()" value="保存"/></td>
            </tr>
        </table>
    </div>
</body>
</html>